package com.example.demo_homework2.controller;

import com.example.demo_homework2.bean.CmsArticle;
import com.example.demo_homework2.common.Response;
import com.example.demo_homework2.service.CmsArtcleService;
import com.fasterxml.jackson.databind.util.ArrayBuilders;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/article")
@Api(tags = "文章信息管理")
public class CmsArticleController {
    @Autowired
    CmsArtcleService artcleService;

    @ApiOperation(value = "修改或创造文章信息", notes = "从数据库修改id所属文章信息或创造文章信息")
    @PostMapping("/saveorupdate")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "文章编号"),
            @ApiImplicitParam(name = "title", value = "文章标题"),
            @ApiImplicitParam(name = "author", value = "文章作者"),
            @ApiImplicitParam(name = "categoryId", value = "所属栏目编号")
    })
    public Response<String> saveOrUpdate(String id, String title, String author, String categoryId) {
        boolean b = artcleService.saveOrUpdate(id, title, author, categoryId);
        if (b) {
            return Response.ok("操作成功");
        } else {
            return Response.error("操作失败");
        }
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除一条文章信息", notes = "从数据库删除id所属文章信息")
    @ApiImplicitParam(name = "id", value = "文章编号")
    public Response<String> deleteArticle(String id) {
        int i = artcleService.deleteArticle(id);
        return Response.ok("成功删除" + 1 + "篇文章");
    }

    @DeleteMapping("/deletebyids")
    @ApiOperation(value = "删除多条文章信息", notes = "从数据库按多个id删除文章信息")
    public Response<String> deleteArticles(String[] ids) {
        int i = artcleService.deleteArticleByIds(ids);
        return Response.ok("成功删除" + i + "篇文章");
    }

    @GetMapping("/searchBy")
    @ApiOperation(value = "按条件检索文章信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "begin", value = "开始日期"),
            @ApiImplicitParam(name = "end", value = "结束日期"),
            @ApiImplicitParam(name = "key", value = "文章标题或作者")
    })
    public Response<List<CmsArticle>> getArticleById(Date begin, Date end, String key) {
        System.out.println("begin" + begin);
        System.out.println("end" + end);
        List<CmsArticle> as = artcleService.searchBy(begin, end, key);
        return Response.ok(as);
    }

    @PutMapping("/updateState")
    @ApiOperation(value = "审核文章")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "artId", value = "文章编号"),
            @ApiImplicitParam(name = "state", value = "状态", allowableValues = "已发布,已驳回")
    })
    public Response<String> updateState(String artId, String state) {
        boolean b = artcleService.updateState(artId, state);
        if (b) {
            return Response.ok("操作成功");
        } else {
            return Response.error("要更新的文章不存在");
        }
    }

}
